package djboom.materials.display.test {
	import djboom.materials.core.DataCore;
	import djboom.materials.display.elements.Diamond;
	import djboom.materials.state.Font;

	import org.casalib.display.CasaSprite;
	import org.casalib.display.CasaTextField;

	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * .
	 * <br>
	 * 
	 * @author DJ BooM 
	 * @since 08.04.2010
	 * 
	 */

	public class Answer extends CasaSprite {

		// ----------------------------------------------------------------------------
		// Константы класса.
		// ----------------------------------------------------------------------------

		private static const TITLE_TFORMAT:TextFormat = new TextFormat(Font.MYRIAD_PRO_COND, 20, 0x004064, null, true);

		// ----------------------------------------------------------------------------
		// Свойства класса.
		// ----------------------------------------------------------------------------

		private var _countField:CasaTextField;
		private var _correct:Boolean;
		private var _clicked:Boolean = false;
		private var _diamond:Diamond;
		private var _answerText:String;

		// ----------------------------------------------------------------------------
		// Конструктор.
		// ----------------------------------------------------------------------------

		public function Answer(answerText:String, correct:Boolean) {
			super();
			
			_answerText = answerText;
			
			_correct = correct;
			
			_countField = new CasaTextField();
			_countField.autoSize = TextFieldAutoSize.LEFT;
			_countField.embedFonts = true;
			_countField.antiAliasType = AntiAliasType.ADVANCED;	
			_countField.defaultTextFormat = TITLE_TFORMAT;			_countField.styleSheet = DataCore.STYLESHEET;
			_countField.htmlText = _answerText;
			_countField.thickness = 50;
			_countField.selectable = false;
			_countField.mouseEnabled = false;
			_countField.x = 25;
			addChild(_countField);
			
			_diamond = new Diamond(Diamond.BLUE);
			_diamond.y = 6;
			addChild(_diamond);
			
			this.buttonMode = true;
			this.useHandCursor = true;
			
			addEventListener(MouseEvent.CLICK, clickAnswer);
			
			addEventListener(Event.REMOVED_FROM_STAGE, clearPage);
		}	 

		// ----------------------------------------------------------------------------
		// Методы класса.
		// ----------------------------------------------------------------------------

		private function clickAnswer(event:MouseEvent):void {
			_clicked = !_clicked;
			
			_diamond.destroy();
			_diamond = null;
			
			if (_clicked) {
				_countField.htmlText = '<span class="strong">' + _answerText + '</span>';
				
				_diamond = new Diamond(Diamond.GREEN);
				_diamond.y = 6;
				addChild(_diamond);
			} else {
				_countField.htmlText = _answerText;
				
				_diamond = new Diamond(Diamond.BLUE);
				_diamond.y = 6;
				addChild(_diamond);
			}
		}

		private function clearPage(event:Event):void {
			_diamond.destroy();
			_diamond = null;
			
			_countField.destroy();
			_countField = null;
		}

		// ----------------------------------------------------------------------------
		// Публичные методы.
		// ----------------------------------------------------------------------------
		
		// ----------------------------------------------------------------------------
		// Публичные свойства.
		// ----------------------------------------------------------------------------

		public function get clicked():Boolean {
			return _clicked;
		}

		public function get correct():Boolean {
			return _correct;
		}
	}
}
